# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# openUBMC is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
import os

from conan import ConanFile
from conan.tools.scm import Git
from conan.tools.files import copy, chdir
from conan.tools.env import VirtualBuildEnv
from conan.tools.layout import basic_layout
from conan.tools.gnu import AutotoolsToolchain, Autotools, AutotoolsDeps

required_conan_version = ">=2.13.0"


class LibyamlConan(ConanFile):
    name = "libyaml"
    license = "Apache License V2.0"
    description = "LibYAML is a YAML parser and emitter library"
    chost = None
    settings = "os", "compiler", "build_type", "arch"
    exports_sources = ["permissions.ini"]
    options = {
        "shared": [True, False]
    }
    default_options = {"shared": True}

    def layout(self):
        basic_layout(self)

    def source(self):
        source = self.conan_data["sources"][self.version]
        git = Git(self)
        git.fetch_commit(url=source["url"], commit=source["branch"])
    
    def generate(self):
        env = VirtualBuildEnv(self)
        env.generate()
        tc = AutotoolsToolchain(self)
        buildenv_vars = self.buildenv.vars(self)
        sysroot = buildenv_vars.get("CONAN_CMAKE_SYSROOT")
        if sysroot:
            tc.extra_cflags.append(f"--sysroot={sysroot}")
        if self.options.shared:
            tc.configure_args.append("--enable-shared") 
        tc.generate()

        deps = AutotoolsDeps(self)
        deps.generate()

    def build(self):
        print("============= libyaml build begin =============")
        with chdir(self, self.source_folder):
            autotools_dynamic = Autotools(self)
            chost = self.buildenv.vars(self).get("CHOST")
            self.run("./bootstrap --chost={}".format(chost))

            autotools_dynamic.configure()
            autotools_dynamic.make()
        print("============= libyaml build end =============")

    def package(self):
        copy(self, "permissions.ini", src=self.source_folder, dst=self.package_folder)
        copy(self, "libyaml.so", src=os.path.join(self.source_folder, "src/.libs"), dst=os.path.join(self.package_folder, "usr/lib64"))
        copy(self, "libyaml-0.so.2", src=os.path.join(self.source_folder,"src/.libs"), dst=os.path.join(self.package_folder, "usr/lib64"))
        copy(self, "libyaml-0.so.2.0.9", src=os.path.join(self.source_folder,"src/.libs"), dst=os.path.join(self.package_folder, "usr/lib64"))
        copy(self, "yaml.h", src=os.path.join(self.source_folder,"include"), dst=os.path.join(self.package_folder, "include"))

    def package_info(self):
        self.cpp_info.libdirs = ["usr/lib64"]
        self.cpp_info.libs = ["libyaml"]
        self.cpp_info.includedirs = ["include"]
